/* QAD_RoutingReport.p - Report Proxy Template                                       */
/* Copyright 1986-2010 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy !!old.p!! for use by QAD Reporting Framework.        */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_ro_det no-undo like ro_det
   field bom_desc   like bom_mstr.bom_desc
   field pt_desc1   like pt_mstr.pt_desc1
   field pt_desc2   like pt_mstr.pt_desc2
   field unique_id  as   integer
.

define temp-table ttReportOptions no-undo
   field skpge     like mfc_ctrl.mfc_logical
   field printcmmt like mfc_ctrl.mfc_logical
.

define temp-table ttComments no-undo
   field cmt_cmmt      like cmt_det.cmt_cmmt
   field ref_unique_id as   integer
.

define dataset dsReportResults for tt_ro_det, ttReportOptions, ttComments.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.

empty temp-table tt_ro_det       no-error.
empty temp-table ttReportOptions no-error.
empty temp-table ttComments      no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_RoutingReport.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle(
         "tt_ro_det", "ro_start", "DefaultValueType", "Session"). 

   run SetFieldMetaParameter in ipReportHandle(
         "tt_ro_det", "ro_start", "DefaultValue", today).

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery     for ro_det.
   define variable effdate       like ro_det.ro_start no-undo.
   assign
      hMainQuery           = query MainQuery:handle
      effdate              = 0
      .

   queryString = "for each ro_det no-lock where ro_det.ro_domain = "
                  + quoter(global_domain).

   run AddSomeConditions in reportHandle
      ("ro_det","ro_start",input-output queryString).

   queryString = queryString + " and ( " + quotor(effdate)
                 + " = ?) or ((" + quoter(effdate)
                 + " <> >) and (ro_start = ? or ro_start <="
                 + quoter(effdate) + ") and"
                 +"(ro_end = > or ro_end >= " + quoter(effdate)
                 +"))) break by ro_routing by ro_op"

   queryString = queryString + ":".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:

      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.
